package com.nh.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nh.entity.BaseEntity;
import com.nh.entity.NgAdmin;
import com.nh.entity.NgDictionary;
import com.nh.service.IBaseService;
import com.nh.utils.Page;
import com.nh.utils.PublicConfing;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class BaseAction extends ActionSupport {

	// 管理重新登录键
	public static final String ADMIN_RELOGIN = "login";

	private IBaseService baseService;

	public NgAdmin currentAdmin;

	/**
	 * 标题
	 */
	private String title;
	private Page pager = new Page();
	private int maxPage = 10;
	private String pageUrl = "adminMainAction!findAll.action";

	// 查询信息
	private Map<String, String> queryInfo = new HashMap<String, String>();

	// 返回s信息
	private Map<String, Object> returnInfo = new HashMap<String, Object>();

	public ActionContext getActionContext() {
		return ActionContext.getContext();
		// return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 管理员生成操作日志
	 * 
	 * @param info
	 */
	public void addAdminLogWrite(String info) {

	}

	/**
	 * @param currentAdmin
	 *            the currentAdmin to set
	 */
	public void setCurrentAdmin(NgAdmin currentAdmin) {
		this.currentAdmin = currentAdmin;
	}

	/**
	 * 根据key查找字典值
	 * 
	 * @param key
	 * @return
	 */
	public String getDictionaryValue(String key) {
		try {
			List<NgDictionary> list = this.getBaseService().findByProperty("dictionaryKey", key, NgDictionary.class);
			if (list.size() > 0) {
				return list.get(0).getDictionaryValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 添加字典
	 * 
	 * @param key
	 * @param value
	 */
	public void putDictionaryValue(String key, String value) {
		List<NgDictionary> list = this.getBaseService().findByProperty("dictionaryKey", key, NgDictionary.class);
		if (list.size() > 0) {
			NgDictionary dictionary = list.get(0);
			dictionary.setDictionaryValue(value);
			this.getBaseService().saveOrUpdate(dictionary, NgDictionary.class);
		} else {
			this.getBaseService().save(new NgDictionary(key, value));

		}
	}

	/**
	 * 
	 * @param criteria
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Criteria criteria, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(criteria, pager.getOffset() + 1, maxPage);

	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(Query query, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(query, pager.getOffset() + 1, maxPage);
	}

	/**
	 * 
	 * @param query
	 *            在线查询条件
	 * @param queryActionURL
	 *            分页用的Action地址
	 * @param maxPage
	 *            第页显示记录数
	 */
	public void queryByPage(String sql, String queryActionURL, int maxPage) {
		this.setPageUrl(queryActionURL);
		pager = this.baseService.findBy(sql, pager.getOffset() + 1, maxPage);
	}

	/**
	 * 得到当前管理员登录对象
	 * 
	 * @return
	 */
	public NgAdmin getCurrentAdmin() {
		NgAdmin beanAdmin = null;
		beanAdmin = (NgAdmin) ActionContext.getContext().getSession().get(PublicConfing.ADMINISTRATOR_ADMIN_OBJ_STRING_CONFIG);
		return beanAdmin;
	}

	public Criteria createCriteria(Class<? extends BaseEntity> clazz) {
		return this.baseService.getBaseDao().getSession().createCriteria(clazz);
	}

	public Query createQuery(String hql) {
		return this.baseService.getBaseDao().getSession().createQuery(hql);
	}

	public IBaseService getBaseService() {
		return baseService;
	}

	@Resource
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Map<String, String> getQueryInfo() {
		return queryInfo;
	}

	public void setQueryInfo(Map<String, String> queryInfo) {
		this.queryInfo = queryInfo;
	}

	public Map<String, Object> getReturnInfo() {
		return returnInfo;
	}

	public void setReturnInfo(Map<String, Object> returnInfo) {
		this.returnInfo = returnInfo;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

}
